package lesson1.tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class TestBase
{
	public static FirefoxDriver driver;

	@BeforeClass
	public void setUp() throws Exception
	{
		// Create a driver
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	}

	@AfterClass
	public void tearDown()
	{
		// Stop a driver
		driver.quit();
	}

	protected void submitRegData()
	{
		// Submit registration data
		driver.findElement(By.name("commit")).click();
	}

	protected void fillRegData(UserData p_userData)
	{
		// Fill registration data
		driver.findElement(By.id("user_login")).click();
		driver.findElement(By.id("user_login")).clear();
		driver.findElement(By.id("user_login")).sendKeys(p_userData.login);
		driver.findElement(By.id("user_password")).click();
		driver.findElement(By.id("user_password")).clear();
		driver.findElement(By.id("user_password")).sendKeys(p_userData.password);
		driver.findElement(By.id("user_password_confirmation")).click();
		driver.findElement(By.id("user_password_confirmation")).clear();
		driver.findElement(By.id("user_password_confirmation")).sendKeys(p_userData.password);
		driver.findElement(By.id("user_firstname")).click();
		driver.findElement(By.id("user_firstname")).clear();
		driver.findElement(By.id("user_firstname")).sendKeys(p_userData.firstName);
		driver.findElement(By.id("user_lastname")).click();
		driver.findElement(By.id("user_lastname")).clear();
		driver.findElement(By.id("user_lastname")).sendKeys(p_userData.lastName);
		driver.findElement(By.id("user_mail")).click();
		driver.findElement(By.id("user_mail")).clear();
		driver.findElement(By.id("user_mail")).sendKeys(p_userData.email);
		driver.findElement(By.id("user_custom_field_values_7")).click();
		driver.findElement(By.id("user_custom_field_values_7")).clear();
		driver.findElement(By.id("user_custom_field_values_7")).sendKeys(p_userData.skypeName);
		driver.findElement(By.id("user_custom_field_values_10")).click();
		driver.findElement(By.id("user_custom_field_values_10")).clear();
		driver.findElement(By.id("user_custom_field_values_10")).sendKeys(p_userData.phone);
		driver.findElement(By.id("user_custom_field_values_28")).click();
		driver.findElement(By.id("user_custom_field_values_28")).clear();
		driver.findElement(By.id("user_custom_field_values_28")).sendKeys(p_userData.department);
		driver.findElement(By.id("user_custom_field_values_31")).click();
		driver.findElement(By.id("user_custom_field_values_31")).clear();
		driver.findElement(By.id("user_custom_field_values_31")).sendKeys(p_userData.occupation);

		// Set language (select field)
		WebElement selectLanguage = driver.findElement(By.id("user_language"));
		Select language = new Select(selectLanguage);
		language.selectByValue(p_userData.language);
	}

	protected void openRegistrationPage()
	{
		// Open the registration page
		driver.findElement(By.cssSelector("a.register")).click();
	}

	protected void openIndexPage()
	{
		// Open the index page
		driver.get("https://redmine.cupidplc.com");
	}

	protected void fillLoginData(UserData p_userData)
	{
		// Login to site
		driver.findElement(By.id("username")).click();
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(p_userData.login);
		driver.findElement(By.id("password")).click();
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys(p_userData.password);
	}

	protected void submitLoginData()
	{
		// Submit login data
		driver.findElement(By.name("login")).click();
	}

}
